//
//  CXYNetWork.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/31.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit
import Alamofire


typealias netCallBack = (Dictionary<String, Any>)->Void




class CXYNetWork: NSObject {
    
}

extension CXYNetWork {
    static func getGiftCatergeryList() {
        
    }
    
    static func getTaoBaoItemList(name:String!,callBack:@escaping netCallBack) {
        var mDict = taobaoBaseP(apiName: "taobao.tbk.item.get",q: name,fields: "title,pict_url,item_url,small_images")
        let sign =  taobao2signMD5(dict: mDict)
        mDict["sign"] = sign
        self.getMethod(parameters: mDict, url: destaobaokeURL,callback: callBack)
    }
    
    static func taobaoBaseP(apiName:String!,q:String!,fields:String!)->Dictionary<String, Any> {
        let dict : Dictionary<String,Any> = [
            "method" : apiName,
            "app_key" : taobaoAppKey,
            "sign_method" : "md5",
            "timestamp" : getTimeStamp(),
            "format" : "json",
            "v" : "2.0",
            "q":q,
            "fields":fields
        ]
        return dict
    }
    
    static func getTimeStamp() ->String {
        let now : Date = Date()
        let dformatter = DateFormatter()
        
        dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return dformatter.string(from: now)
    }
}

extension CXYNetWork {
    static func taobao2signMD5(dict:Dictionary<String, Any>) -> String {
        let temArray = Array(dict.keys)
        let compareArray = temArray.sorted()
        
        var keyStr:String = taobaoAppSecret
        
        for i in 0...(compareArray.count - 1) {
            keyStr = keyStr + (compareArray[i])
            keyStr = keyStr + (dict[compareArray[i]] as! String)
        }
        
        keyStr = keyStr + taobaoAppSecret
        let md5Hex = md5String(str: keyStr)
        return md5Hex
    }
    
    static func md5String(str:String) -> String {
        
        let cStr = str.cString(using: String.Encoding.utf8);
        
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
        
        CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer)
        
        let md5String = NSMutableString();
        for i in 0 ..< 16 {
            md5String.appendFormat("%02X", buffer[i])
        }
        
        free(buffer)
        return md5String as String
    }
}

extension CXYNetWork {
    static func getMethod (parameters:Parameters!,url:String!,callback: @escaping netCallBack) {
        Alamofire.request(url, method: .get, parameters: parameters , encoding: URLEncoding.default, headers: nil).responseJSON(completionHandler: { (data) in
            let dict = data.result.value;
            callback(dict as! Dictionary<String, Any>);
        })
    }
    
    static func postMehod(parameters:Parameters!,url:String!,callback:@escaping netCallBack) {
        Alamofire.request(url, method: .post, parameters: parameters as Parameters , encoding: URLEncoding.default).responseJSON(completionHandler: { (data) in
            let dict = data.result.value;
            callback(dict as! Dictionary<String, Any>);
        })
    }
}
